//
//  RunLoopTestViewController.m
//  RunLoop
//
//  Created by user on 16/4/15.
//  Copyright © 2016年 user. All rights reserved.
//

#import "RunLoopTestViewController.h"

@interface RunLoopTestViewController ()
@property(nonatomic,strong)NSThread *thead;
@end

@implementation RunLoopTestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self threadRun];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self performSelector:@selector(test) onThread:self.thead withObject:nil waitUntilDone:NO];
    
    
}
-(void)test
{
     NSLog(@"--------run %@------",[NSThread currentThread]);
}
// 常驻线程
-(void)threadRun
{
    self.thead = [[NSThread alloc]initWithTarget:self selector:@selector(runThread) object:nil];
    [self.thead start];
}

-(void)runThread
{
    NSLog(@"--------run %@------",[NSThread currentThread]);
    /*第一种做法(建议使用第一次)*/
//    [[NSRunLoop currentRunLoop]addPort:[NSPort port] forMode:NSDefaultRunLoopMode];
//    //无mode runloop 直接退出
//    [[NSRunLoop currentRunLoop]run];
    /*第二种做法*/
//    while (1) {
//        [[NSRunLoop currentRunLoop]run];
//    }
//    NSLog(@"--------run------");
    /**
     *子线程 定时器
     */
//    NSTimer *timer  = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(timeRun) userInfo:nil repeats:YES];
//    [[NSRunLoop currentRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];
//    [[NSRunLoop currentRunLoop]run];
    //自动释放池：在runloop睡眠之前释放 在进入时创建
    @autoreleasepool {
        [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeRun) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] run];
    }
    
}
-(void)timeRun
{
    NSLog(@"-----time1----");
    
}
//定时器
-(void)time1
{
    
    NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(run) userInfo:nil repeats:YES];
    
    //NSDefaultRunLoopMode:系统默认mode
    //UITrackingRunLoopMode：监听滑动mode scrollview
    /**
     *CommonModes:标记 占位 不是真正的模式包括（NSDefaultRunLoopMode,UITrackingRunLoopMode）
     */
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    
}
-(void)time2
{
    //默认添加到defalutmode
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(run) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
}
-(void)run
{
    
    NSLog(@"run");
}

//延迟加载图片
-(void)userImageView
{
//    [self.imageview performSelector:@selector(setImage:) withObject:[uiimageView plohderImageView:@""] afterDelay:1.0 inModes:@[NSDefaultRunLoopMode]];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
